package com.example.androidlearning2400.ui.comp

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import com.example.androidlearning2400.ui.model.BannerBean
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

@Composable
fun BannerSlider(
    bannerList: List<BannerBean>,
    autoPlay: Boolean = true,
    autoPlayDelay: Long = 3000,
    bannerHeight: Int = 220,
    onBannerClick: (BannerBean) -> Unit = {}
) {
    if (bannerList.isEmpty()) return

    val realCount = bannerList.size
    val scope = rememberCoroutineScope()
    val pagerState = rememberPagerState(initialPage = 0, pageCount = { Int.MAX_VALUE })
    val realIndex by remember(pagerState.currentPage) {
        mutableIntStateOf((pagerState.currentPage % realCount + realCount) % realCount)
    }

    // 自动播放
    LaunchedEffect(autoPlay, autoPlayDelay, realCount) {
        if (!autoPlay) return@LaunchedEffect
        while (isActive) {
            delay(autoPlayDelay)
            scope.launch { pagerState.animateScrollToPage(pagerState.currentPage + 1) }
        }
    }

    Box(modifier = Modifier.fillMaxWidth().height(bannerHeight.dp)) {
        HorizontalPager(state = pagerState, userScrollEnabled = true) { page ->
            val index = (page % realCount + realCount) % realCount
            val item = bannerList[index]

            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(bannerHeight.dp)
                    .clickable { onBannerClick(item) },
                contentAlignment = Alignment.BottomCenter
            ) {
                // 图片
                androidx.compose.foundation.Image(
                    painter = rememberAsyncImagePainter(item.imgUrl),
                    contentDescription = "轮播图",
                    modifier = Modifier.fillMaxSize(),
                    contentScale = ContentScale.Crop
                )

                // 1. 黑色文字条（最底下）
                Column(
                    modifier = Modifier
                        .align(Alignment.BottomCenter)
                        .fillMaxWidth()
                        .background(Color(0x80000000))
                        .padding(vertical = 8.dp)
                ) {
                    Text(
                        text = item.text,
                        fontSize = 16.sp,
                        color = Color.White,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(horizontal = 12.dp),
                        textAlign = TextAlign.Center
                    )
                }

                // 2. 指示器（文字上方 12dp）
                Row(
                    modifier = Modifier
                        .align(Alignment.BottomCenter)
                        .offset(y = -48.dp)   // 往上提，避免压住文字
                        .padding(bottom = 8.dp)
                ) {
                    repeat(realCount) { idx ->
                        Box(
                            modifier = Modifier
                                .padding(horizontal = 4.dp)
                                .clip(CircleShape)
                                .background(
                                    if (idx == realIndex) Color.Cyan
                                    else Color.White.copy(alpha = 0.5f)
                                )
                                .height(8.dp)
                                .width(if (idx == realIndex) 24.dp else 8.dp)
                        )
                    }
                }
            }
        }
    }
}